package vip.youngboy.freejump.handler.task;

import org.activiti.engine.task.Task;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import vip.youngboy.freejump.command.ReportActionCommand;
import vip.youngboy.freejump.constant.TaskActionType;
import vip.youngboy.freejump.domain.FlowRequestParam;
import vip.youngboy.freejump.domain.TaskActionModel;
import vip.youngboy.freejump.domain.process.Option;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Component
public class TaskReportActionHandler extends AbstractTaskActionHandler {

    @Override
    public void doHandleAction(TaskActionModel taskActionModel) {
        //执行上报逻辑
        FlowRequestParam flowRequestParam = taskActionModel.getFlowRequestParam();
        Task task = taskActionModel.getTask();
        String taskId = task.getId();
        //解析前端选择的人员信息
        Set<String> candidateUsers = parseUserOptions(flowRequestParam);
        //修改当前任务的候选人
        taskService.setAssignee(task.getId(), null);
        if (candidateUsers.size() == 1) {
            Iterator<String> iterator = candidateUsers.iterator();
            taskService.setAssignee(taskId, iterator.next());
        } else {
            //执行上报命令类
            executeCommand(new ReportActionCommand(taskId, candidateUsers));
        }
    }


    @Override
    public void vaildOptions(FlowRequestParam flowRequestParam) {
        Assert.notNull(flowRequestParam.getUserOptions(), "用户选择结果不能为空");
    }

    @Override
    public TaskActionType supportAction() {
        return TaskActionType.REPORT;
    }

    @Override
    public List<Option> getOptions() {
        return null;
    }

}
